﻿/*******************************************************************************
Copyright (c) 2011-2013, PKStudio
Copyright (c) 2011,      John Wordsworth
Copyright (c) 2011,      Zynga Inc.
Copyright (c) 2008-2010, Ricardo Quesada

http://pkstudio.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace PKEngine
{
	/// <summary>
	/// Singleton that manages the Animations.
	/// </summary>
	public class PKAnimationCache
	{
		#region Private Fields
		private Dictionary<string, PKAnimation> cachedAnimations;
		#endregion

		#region Singleton
		private static PKAnimationCache _sharedAnimationCache;

		public static PKAnimationCache SharedAnimationCache
		{
			get
			{
				if (_sharedAnimationCache == null)
				{
					_sharedAnimationCache = new PKAnimationCache();
				}
				return _sharedAnimationCache;
			}
		}
		#endregion

		#region Initialization
		private PKAnimationCache()
		{
			cachedAnimations = new Dictionary<string, PKAnimation>(24);
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Adds a PKAnimation with a name.
		/// </summary>
		/// <param name="animation">Animation</param>
		/// <param name="name">Name of animation</param>
		public void AddAnimation(PKAnimation animation, string name)
		{
			Debug.Assert(animation != null, "PKAnimationCache.AddAnimation: Argument cannot be null.");

			cachedAnimations.Add(name, animation);
		}

		/// <summary>
		/// Removes all animations.
		/// </summary>
		public void ClearAnimations()
		{
			cachedAnimations.Clear();
		}

		/// <summary>
		/// Removes a PKAnimation.
		/// </summary>
		/// <param name="name">Name of animation</param>
		public void RemoveAnimation(string name)
		{
			if (string.IsNullOrEmpty(name))
			{
				return;
			}

			cachedAnimations.Remove(name);
		}

		/// <summary>
		/// Gets a PKAnimation by name.
		/// </summary>
		/// <param name="name">Name of animation</param>
		/// <returns>The animation with specified name</returns>
		public PKAnimation GetAnimation(string name)
		{
			if (cachedAnimations.ContainsKey(name))
			{
				return cachedAnimations[name];
			}
			return null;
		}
		#endregion
	}
}
